package com.dragon.app.controller.attendance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.avttendance.IAttendanceService;
import com.dragon.repository.domain.dto.ArrangePeriodDTO;
import com.dragon.repository.domain.dto.AttendanceDTO;
import com.dragon.repository.domain.dto.AttendanceLeaveDTO;
import com.dragon.repository.domain.vo.AttendanceVO;
import com.dragon.repository.entity.Attendance;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * <p>
 * 考勤表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-10
 */
@RestController
@RequestMapping("attendance")
@Api(tags = "考勤打卡")
public class AttendanceController {

    @Autowired
    IAttendanceService service;

    @GetMapping("clockList")
    @ApiOperation(value = "打卡列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId",value = "用户id",required = false,type = "int"),
            @ApiImplicitParam(name = "time", value = "选择时间", required = false, type = "string"),
            @ApiImplicitParam(name = "attendanceType", value = "打卡状态", required = false, type = "int")
    })
    public ResponseData<Map<String, List<Attendance>>> clockList(@RequestParam(required = false) Integer adminId,
                                                                 @RequestParam(required = false) String time,
                                                                 @RequestParam(required = false) Integer attendanceType){
        Map<String, List<Attendance>> attendanceMap = service.clockList(adminId,time,attendanceType);
        return ResponseData.success(attendanceMap);
    }

    @PostMapping("inClock")
    @ApiOperation(value = "上班打卡")
    public ResponseData clockJob(@RequestBody AttendanceVO vo) {
        service.adminInClock(AttendanceVO.convert(vo));
        return ResponseData.success();
    }


    @GetMapping("getAttendList")
    @ApiOperation(value = "统计考勤天数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminIds", value = "用户idList", required = false, type = "array"),
            @ApiImplicitParam(name = "time", value = "时间",required = false,type = "string")
    })
    public ResponseData getAttendList(@RequestParam(required = false) List<Integer> adminIds ,
                                      @RequestParam(required = false) String time) {
        List<Map<String,Object>> calendars = service.getAttendMap(adminIds,time);
        return ResponseData.success(calendars);
    }

    @PostMapping("showDetails")
    @ApiOperation(value = "获取用户某天打卡详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "用户id",required = true, type = "int"),
            @ApiImplicitParam(name = "time", value = "年月日",required = false, type = "string")
    })
    public ResponseData<AttendanceDTO> showOneDetails(@RequestParam Integer adminId, @RequestParam(required = false) String time){
        Attendance attendance = service.showDetails(adminId,time);
        AttendanceDTO attendanceDTO = AttendanceDTO.convert(attendance);
        //attendanceDTO.setAttendanceChildList(AttendanceChildDTO.convertList(attendance.getAttendanceChildList()));
        return ResponseData.success(attendanceDTO);
    }

    @PostMapping("determine")
    @ApiOperation(value = "判断经纬度")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "用户id", required = true, type = "int"),
            @ApiImplicitParam(name = "actualLongitude", value = "当前经度", required = true, type = "double"),
            @ApiImplicitParam(name = "actualLatitude", value = "当前纬度", required = true, type = "double")
    })
    public ResponseData determine(Integer adminId, Double actualLongitude, Double actualLatitude){
        return ResponseData.success(service.determine(adminId, actualLongitude, actualLatitude));
    }

    @PostMapping("list")
    @ApiOperation(value = "列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "adminId", value = "用户id", required = true, type = "int"),
            @ApiImplicitParam(name = "time", value = "时间", required = false, type = "string")
    })
    public ResponseData<List<Attendance>> list(Integer page, Integer limit, Integer adminId, String time){
        Page<Attendance> attendancePage = service.attList(page, limit, adminId, time);
        return ResponseData.success(attendancePage.getRecords());
    }


    @GetMapping("monthList")
    @ApiOperation(value = "获取某人某月列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "id",required = true,type = "int"),
            @ApiImplicitParam(name = "time", value = "年月", required = false, type = "int")
    })
    public ResponseData<Map<String, Object>> ScheduleList(@RequestParam Integer adminId, @RequestParam(required = false) String time){
        Map<String, Object> list = service.getList(adminId,time);
        return ResponseData.success(list);
    }


    @GetMapping("details")
    @ApiOperation(value = "考勤某天详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "id",required = true,type = "int"),
            @ApiImplicitParam(name = "time", value = "年月", required = true, type = "int")
    })
    public ResponseData selectArrangeChildDetail(@RequestParam Integer adminId, @RequestParam String time){
        Attendance attendance = service.selectArrangeChildDetail(adminId,time);
        AttendanceDTO attendanceDTO = AttendanceDTO.convert(attendance);
        attendanceDTO.setPeriodList(ArrangePeriodDTO.convertList(attendance.getPeriodList()));
        //attendanceDTO.setAttendanceChildDTOList(AttendanceChildDTO.convertList(attendance.getAttendanceChildList()));
        attendanceDTO.setAttendanceLeave(AttendanceLeaveDTO.convertList(attendance.getAttendanceLeave()));
        return ResponseData.success(attendanceDTO);
    }


}
